package com.black.play.component.base;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ylx
 */
public class AppMenu extends JPanel {
    private final GridBagConstraints gridBagConstraints = new GridBagConstraints();
    private final Map<String, AppButton> buttonMap = new HashMap<>();

    public AppMenu() {
        setLayout(new GridBagLayout());
        gridBagConstraints.fill = GridBagConstraints.VERTICAL;
        gridBagConstraints.anchor = GridBagConstraints.CENTER;
        gridBagConstraints.gridy = 0;
    }

    public void addMenu(String menuName, ActionListener actionListener) {
        AppButton appButton = new AppButton(menuName);
        appButton.addActionListener(actionListener);
        add(appButton, gridBagConstraints);
        buttonMap.put(menuName, appButton);
        gridBagConstraints.gridy++;
    }

    public void remove(String menuName) {
        AppButton appButton = buttonMap.get(menuName);
        if (appButton != null) {
            remove(appButton);
        }
    }

    public void update(String menuName, String menuNameNew){
        buttonMap.get(menuName).setText(menuNameNew);
    }
}
